#include <iostream>
#include <string>
#include "mylist.h"

void print_list(const std::string& msg, mylist<int>& list) {
    std::cout << msg << std::endl;
    std::cout << "  Size: " << list.get_size();
    std::cout << " | Empty: " << (list.empty() ? "true" : "false") << std::endl;
    std::cout << "  Contents: ";
    if (list.empty()) {
        std::cout << "<empty>";
    } else {
        for (int & it : list) {
            std::cout << it << " ";
        }
    }
    std::cout << std::endl << "----------------------------------------" << std::endl;
}

void print_list_reverse(const std::string& msg, mylist<int>& list) {
    std::cout << msg << std::endl;
    std::cout << "  Reverse Contents: ";
    if (list.empty()) {
        std::cout << "<empty>";
    } else {
        mylist<int>::iterator it = list.end();
        while (it != list.begin()) {
            --it;
            std::cout << *it << " ";
        }
    }
    std::cout << std::endl << "----------------------------------------" << std::endl;
}


int main() {
    std::cout << "### test 1: push_back, empty, get_size ###" << std::endl;
    mylist<int> list1;
    print_list("Initial empty list (list1)", list1);

    list1.push_back(10);
    list1.push_back(20);
    list1.push_back(30);
    print_list("After push_back {10, 20, 30}", list1);

    std::cout << "\n### test 2: push_front ###" << std::endl;
    list1.push_front(5);
    list1.push_front(0);
    print_list("After push_front {5, 0}", list1);

    std::cout << "\n### test 3: pop_back ###" << std::endl;
    list1.pop_back();
    print_list("After pop_back (removed 30)", list1);
    list1.pop_back();
    print_list("After pop_back (removed 20)", list1);

    std::cout << "\n### test 4: pop_front ###" << std::endl;
    list1.pop_front();
    print_list("After pop_front (removed 0)", list1);
    list1.pop_front();
    print_list("After pop_front (removed 5)", list1);
    list1.pop_front();
    print_list("After pop_front (removed 10)", list1);

    std::cout << "\n### test 5: insert ###" << std::endl;
    list1.push_back(100);
    list1.push_back(300);
    print_list("Repopulated list {100, 300}", list1);

    mylist<int>::iterator it = list1.begin();
    list1.insert(it, 50);
    print_list("After insert at begin (50)", list1);

    it = list1.end();
    list1.insert(it, 400);
    print_list("After insert at end (400)", list1);

    // 在中间插入
    // it = list1.begin();
    ++it; // -> 100
    ++it; // -> 300
    list1.insert(it, 200);
    print_list("After insert in middle (200 before 300)", list1); // 预期: 50 100 200 300 400

    // 6. 测试: erase(iterator)
    std::cout << "\n### test 6: erase (single iterator) ###" << std::endl;

    it = list1.erase(it);
    print_list("After erase begin (removed 50)", list1);

    std::cout << "  Iterator now points to: ";
    // *** [添加这个检查] ***
    if (it != list1.end())
    {
        std::cout << *it << std::endl;
    }
    else
    {
        std::cout << "end()" << std::endl;
    }
    // 在 begin() 处擦除
    it = list1.begin();
    it = list1.erase(it); // 'it' 应该指向 100
    print_list("After erase begin (removed 50)", list1);
    if (it != list1.end()) {
        std::cout << *it << std::endl;
    } else {
        std::cout << "end()" << std::endl;
    }
    std::cout << "  Iterator now points to: ";
    // *** [添加这个检查] ***
    if (it != list1.end())
    {
        std::cout << *it << std::endl;
    }
    else
    {
        std::cout << "end()" << std::endl;
    }

    // 在中间擦除
    it = list1.begin();
    ++it; // -> 200
    it = list1.erase(it); // 'it' 应该指向 300
    print_list("After erase middle (removed 200)", list1);
    std::cout << "  Iterator now points to: " << *it << std::endl; // 预期: 300

    // 擦除最后一个元素
    ++it; // -> 400
    it = list1.erase(it); // 'it' 应该指向 end()
    print_list("After erase last element (removed 400)", list1);
    if (it == list1.end()) {
        std::cout << "  Iterator is correctly at end()" << std::endl;
    }

    // 7. 测试: erase(first, last)
    std::cout << "\n### test 7: erase (range) ###" << std::endl;
    list1.push_front(5); // list1 是 {5, 100, 300}
    list1.push_back(400); // list1 是 {5, 100, 300, 400}
    list1.push_back(500); // list1 是 {5, 100, 300, 400, 500}
    print_list("Repopulated list for range erase", list1);

    mylist<int>::iterator first = list1.begin();
    mylist<int>::iterator last = list1.end();
    ++first; // -> 100
    --last; // -> 500
    // 擦除 [100, 300, 400]. 'last' 指向 500 (开区间)
    it = list1.erase(first, last);
    print_list("After erase range [100, 300, 400]", list1); // 预期: 5 500
    std::cout << "  Iterator now points to: " << *it << std::endl; // 预期: 500

    // 擦除所有
    it = list1.erase(list1.begin(), list1.end());
    print_list("After erase all", list1);
    if (it == list1.end()) {
        std::cout << "  Iterator is correctly at end()" << std::endl;
    }

    // 8. 测试: operator= (拷贝赋值)
    std::cout << "\n### test 8: operator= (Copy Assignment) ###" << std::endl;
    list1.push_back(1); list1.push_back(2); list1.push_back(3);
    mylist<int> list2;
    list2.push_back(99);
    print_list("list1 before assignment", list1);
    print_list("list2 before assignment", list2);

    list2 = list1;
    print_list("list2 after assignment (list2 = list1)", list2);

    // 测试深拷贝
    list1.pop_front();
    print_list("list1 after pop_front (testing deep copy)", list1);
    print_list("list2 should be unchanged", list2); // 预期: 1 2 3

    // 测试自赋值
    std::cout << "Testing self-assignment..." << std::endl;
    list1 = list1;
    print_list("list1 after self-assignment", list1); // 预期: 2 3

    // 9. 测试: Copy Constructor (拷贝构造)
    std::cout << "\n### test 9: Copy Constructor ###" << std::endl;
    print_list("list2 state", list2); // 预期: 1 2 3
    mylist<int> list3(list2);
    print_list("list3 (copy constructed from list2)", list3);

    // 测试深拷贝
    list2.push_front(0);
    print_list("list2 after push_front (testing deep copy)", list2);
    print_list("list3 should be unchanged", list3); // 预期: 1 2 3

    // 10. 测试: splice()
    std::cout << "\n### test 10: splice ###" << std::endl;
    mylist<int> listA; listA.push_back(100); listA.push_back(200);
    mylist<int> listB; listB.push_back(1); listB.push_back(2);
    print_list("listA before splice", listA);
    print_list("listB before splice", listB);

    // 在开头拼接
    listA.splice(listA.begin(), listB);
    print_list("listA after splice(begin, listB)", listA); // 预期: 1 2 100 200
    print_list("listB after splice (should be empty)", listB);

    // 在中间拼接
    listB.push_back(55); listB.push_back(66);
    print_list("listB repopulated", listB);
    it = listA.begin();
    ++it; // -> 2
    ++it; // -> 100
    listA.splice(it, listB);
    print_list("listA after splice(middle, listB)", listA); // 预期: 1 2 55 66 100 200
    print_list("listB after splice (should be empty)", listB);

    // 11. 测试: Iterator (后缀和反向)
    std::cout << "\n### test 11: Iterator operators ###" << std::endl;
    // 测试反向迭代 (前置--)
    print_list_reverse("Testing reverse iteration (--) on listA", listA);

    // 测试后缀++ 和 --
    std::cout << "\nTesting post-increment/decrement:" << std::endl;
    it = listA.begin(); // -> 1
    std::cout << "  *(it++): " << *(it++) << " (Should be 1)" << std::endl;
    std::cout << "  *it:     " << *it     << " (Should be 2)" << std::endl;
    it = listA.end();
    --it; // -> 200
    std::cout << "  *(it--): " << *(it--) << " (Should be 200)" << std::endl;
    std::cout << "  *it:     " << *it     << " (Should be 100)" << std::endl;

    std::cout << "\n\nAll tests completed." << std::endl;

    return 0;
}